package com.example.controller;

import com.example.Utils.CommonStringUtils;
import com.example.Utils.DateUtils;
import com.example.models.Education;
import com.example.models.User;
import com.example.struts.HttpParams;
import com.google.gson.Gson;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

@Controller
public class InterfaceBackGroundController {
    @RequestMapping(value = "/background/get_user_info_by_id/")
    @ResponseBody
    public String getUserInfoById(HttpServletRequest request) {
        HttpParams params = new HttpParams(request);
        Integer userId = params.getIntParam("user_id",0);
        User user = User.find.byId(userId);
        Map<String,String> userInfo = new HashMap<>();
        if(user != null){
            userInfo.put("user_id", CommonStringUtils.safeString(user.id));
            userInfo.put("img_path",user.headImgPath);
            userInfo.put("username",user.username);
            userInfo.put("province",user.province);
            userInfo.put("brief",user.brief);
            userInfo.put("mobile",user.mobile);
            userInfo.put("gender",user.gender.getCname());
            userInfo.put("birthday", DateUtils.DateToStr(user.birthday,"yyyy-MM-dd"));
        }
        Logger.getGlobal().info(String.format("/background/get_user_info_by_id/?%s",userId));
        return CommonStringUtils.encodeString(new Gson().toJson(userInfo),"UTF-8");
    }

    @RequestMapping(value = "/background/get_user_info_by_ids/")
    @ResponseBody
    public String getUserInfoByIds(HttpServletRequest request) {
        HttpParams params = new HttpParams(request);
        String userIdStr = params.getParam("user_ids","");
        List<Integer> userIds = Arrays.stream(userIdStr.split(",")).map(CommonStringUtils::safeInteger).collect(Collectors.toList());
        List<User> users = User.search().in("id",userIds).findList();
        Map<Integer,Map<String,String>> resMap = new HashMap<>();
        for (User user : users) {
            Map<String, String> userInfo = new HashMap<>();
            userInfo.put("user_id", CommonStringUtils.safeString(user.id));
            userInfo.put("img_path", user.headImgPath);
            userInfo.put("username", user.username);
            resMap.put(user.id,userInfo);
        }
        Logger.getGlobal().info(String.format("/background/get_user_info_by_ids/?%s",userIdStr));
        return CommonStringUtils.encodeString(new Gson().toJson(resMap),"UTF-8");
    }

    @RequestMapping(value = "/background/get_educations_info/",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getEducations(HttpServletRequest request) {
        HttpParams params = new HttpParams(request);
        String province = params.getParam("province");
        String city = params.getParam("city");
        params.printParameter();
        List<Map<String,String>> resList = new ArrayList<>();
        if(CommonStringUtils.isNullOrEmpty(province) || CommonStringUtils.isNullOrEmpty(city)){
            return new Gson().toJson(resList);
        }
        if(province.endsWith("省")){
            province = province.substring(0,province.length()-1);
        }
        if(city.endsWith("市")){
            city = city.substring(0,city.length()-1);
        }
        List<Education> educations = Education.search().icontains("province",province).icontains("city",city).eq("deleted",false).findList();
        educations.forEach(education -> {
            Map<String,String> map = new HashMap<>();
            map.put("school_name",education.eduName);
            resList.add(map);
        });
        return new Gson().toJson(resList);
    }
}
